# import numpy as np
# import netCDF4 as nc
# import pandas as pd
#
# # 加载云顶高度数据
# nf = nc.Dataset(r'/home/liudd/data/fy4a_lpw/FY4A-_AGRI--_N_DISK_1047E_L2-_LPW-_MULT_NOM_20190217070000_20190217071459_4000M_V0001.NC', 'r')
# lpw_data = nf.variables['LPW_MID'][:]
#
# # 加载地理坐标数据
# coord_file_name = '/home/liudd/data_preprocessing/FY4A_coordinates.nc'
# coord_file_open = nc.Dataset(coord_file_name, 'r')
# lat = coord_file_open.variables['lat'][:, :].T
# lon = coord_file_open.variables['lon'][:, :].T
#
# # 获取行列号
# rows, cols = np.indices(lpw_data.shape)
#
# # 将数据整理成Pandas DataFrame
# data = {
#     'Latitude': lat.flatten(),
#     'Longitude': lon.flatten(),
#     'Row': rows.flatten(),
#     'Column': cols.flatten(),
#     'LPW': lpw_data.flatten()
# }
#
# df = pd.DataFrame(data)
#
# # 过滤掉包含NaN值的行
# df.dropna(inplace=True)
#
# # 输出到CSV文件
# csv_file_path = 'fy20190228lpw.csv'
# df.to_csv(csv_file_path, index=False)
#
# print(f'Data saved to {csv_file_path}')
import numpy as np
import netCDF4 as nc
import pandas as pd

# 加载云顶高度数据
nf = nc.Dataset(r'/home/liudd/data/fy4a_lpw/FY4A-_AGRI--_N_DISK_1047E_L2-_LPW-_MULT_NOM_20190217070000_20190217071459_4000M_V0001.NC', 'r')
lpw_data = nf.variables['LPW_MID'][:]

# 加载地理坐标数据
coord_file_name = '/home/liudd/data_preprocessing/FY4A_coordinates.nc'
coord_file_open = nc.Dataset(coord_file_name, 'r')
lat = coord_file_open.variables['lat'][:, :].T
lon = coord_file_open.variables['lon'][:, :].T

# 获取行列号
rows, cols = np.indices(lpw_data.shape)

# 将地理坐标、行列号及LPW数据整理成Pandas DataFrame
data = {
    'Latitude': lat.flatten(),
    'Longitude': lon.flatten(),
    'Row': rows.flatten(),
    'Column': cols.flatten(),
    'LPW': lpw_data.flatten()
}
df = pd.DataFrame(data)

# 过滤掉包含NaN值的行
df.dropna(inplace=True)

# 加载cloudsat匹配文件中的纬度和经度数据
cloudsat_df = pd.read_csv('cloudsat_fy4a_matches.csv')

# 将`cloudsat_df`中的Latitude和Longitude与LPW数据中的相匹配
merged_df = pd.merge(cloudsat_df, df, left_on=['fy_lat', 'fy_lon'], right_on=['Latitude', 'Longitude'], how='inner')

# 输出到CSV文件
csv_file_path = 'matched_lpw.csv'
merged_df.to_csv(csv_file_path, index=False)

print(f'Data saved to {csv_file_path}')
